package com.factory.test.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

public class TouchView extends View {

    public TouchView(Context context) {
        super(context);
        initView(context);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        initView(context);
    }


    int widthPixel;
    int heightPixel;
    int step = 100;
    private Paint paint;
    private Paint paintTouch;
    private Paint linePaint;
    private void initView(Context context){
        DisplayMetrics outMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
        widthPixel = outMetrics.widthPixels;
        heightPixel = outMetrics.heightPixels;

        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);       //设置非填充
        paint.setStrokeWidth(5);                //笔宽5像素
        paint.setColor(Color.parseColor("#D7D7D7"));
        paint.setAntiAlias(true);               //锯齿不显示

        paintTouch = new Paint();
        paintTouch.setStyle(Paint.Style.FILL);       //设置非填充
        paintTouch.setStrokeWidth(5);                //笔宽5像素
        paintTouch.setColor(Color.GREEN);
        paintTouch.setAntiAlias(true);               //锯齿不显示

        linePaint = new Paint();
        linePaint.setStyle(Paint.Style.FILL);       //设置非填充
        linePaint.setStrokeWidth(1);                //笔宽5像素
        linePaint.setColor(Color.parseColor("#FFFFFF"));
        linePaint.setAntiAlias(true);               //锯齿不显示


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < heightPixel; i += step) {
            for (int j = 0; j < widthPixel; j += step) {
                Rect r = new Rect(j, i, j + step, i + step);
                int x = j / step;
                int y = i / step;

                String key = formatKey(x, y);
                if(map.get(key) != null){
                    canvas.drawRect(r, paintTouch);
                }else{
                    canvas.drawRect(r, paint);
                }

                canvas.drawLine(r.left, r.top, r.right, r.top, linePaint);
                canvas.drawLine(r.left, r.bottom, r.right, r.bottom, linePaint);
                canvas.drawLine(r.left, r.top, r.left, r.bottom, linePaint);
                canvas.drawLine(r.right, r.top, r.right, r.bottom, linePaint);
            }
        }
    }

    Map<String, Boolean> map = new HashMap<>();

    private String lastKey = null;
    private String formatKey(int x, int y){
        return x + "_" + y;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:{

            }
            case MotionEvent.ACTION_MOVE:{
                int x = (int) event.getX();
                int y = (int)  event.getY();
                int xOffset = x / step;
                int yOffset = y / step;
                String key = formatKey(xOffset, yOffset);
                if(key.equals(lastKey)){
                    break;
                }
                lastKey = key;
                map.put(key, true);
                invalidate();
                break;
            }
        }

        return true;
    }

    private void findPosition(int x, int y){

    }
}
